#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

void new();
void dis();
void sor();
void rev();
int cou(int);
int ind(int);
void app(int);
void pop(int);
void ext();
void ins(int,int);
void rem(int);

int *a=NULL,len=0;

void main()
{
  int ch,flag=0,num,pos,x;
  
  do
  {
    printf("\nCHOOSE:\n1.New\n2.Display\n3.Sort\n4.Reverse\n5.Count\n6.Index\n7.Append\n8.Pop\n9.Remove\n10.Extend\n11.Insert\n12.Quit\n");
    scanf("%d",&ch);
  
    if(flag==0 && ch==1 && len==0)
    {
      flag=1;
    }
    else if(len==0 && ch>1 && ch<11 && flag==0)
    {
      printf("No existing List !!!");
      continue;
    }
  
    switch(ch)
    { 
      case 1:new();break;
      case 2:dis();break;
      case 3:sor();break;
      case 4:rev();break;
      case 5:printf("No. to count : ");scanf("%d",&num);x=cou(num);printf("The count is : %d",x);break;
      case 6:printf("No. to find index for : ");scanf("%d",&num);x=ind(num);if(x==-1)printf("Not Found !!!");else printf("The index is : %d",x);break;
      case 7:printf("Enter the value you want to append with : ");scanf("%d",&num);app(num);break;
      case 8:printf("Index to pop (-1 for default) ? : ");scanf("%d",&pos);pop(pos);break;
      case 9:printf("No. to remove ? : ");scanf("%d",&num);rem(num);break;
      case 10:ext();break;
      case 11:printf("No. to insert ? : ");scanf("%d",&num);printf("Index to insert in ? : ");scanf("%d",&pos);ins(num,pos);break;
      case 12:exit(0);break;
      default:printf("Choose Wisely ! Live Well...");
    }
  }while(1);
}

void new()
{
  int num,flag=0;
  char c;
  printf("Start Entering Values...");
  do
  {
    printf("Enter : ");
    scanf("%d",&num);
    if(flag==0)
    {
      a=malloc(sizeof(int));
      flag=1;
    }
    else
      a=realloc(a,(len+1)*sizeof(int));
    len++;
    a[len-1]=num;
    printf("another one ? (y/n) : ");
    scanf("%s",&c);    
  }while(c=='y');
  dis();
}

void dis()
{
  int i;
  printf("\nThe list is -> ");
  for(i=0;i<len;i++)
  {
    printf("%d ",a[i]);
  }
}

int cou(int num)
{
  int i,count=0;
  for(i=0;i<len;i++)
  {
    if(a[i]==num)
      count++;
  }
  
  return count;
}

int ind(int num)
{
  int i,pos=-1;
  for(i=0;i<len;i++)
  {
    if(a[i]==num)
    {
      pos=i;
      break;
    }
  }
  
  return pos;
}

void sor()
{
  int i,j,tmp;
  
  for(i=0;i<len-1;i++)
  {
    for(j=i+1;j<len;j++)
    {
      if(a[i]>a[j])
      {
	tmp=a[j];
	a[j]=a[i];
	a[i]=tmp;
      }
    }
  }
  dis();
}

void app(int num)
{
  realloc(a,(len+1)*sizeof(int));
  a[len]=num;
  len++;
  dis();
}

void ext()
{  
  int num;
  char c;
  printf("Start Entering Values for secondary list...");
  do
  {
    printf("Enter : ");
    scanf("%d",&num);
    len++;
    realloc(a,len*sizeof(int));
    a[len-1]=num;
    printf("another one ? (y/n) : ");
    scanf("%s",&c);
  }while(c=='y' && len<100);
  
  dis();
}

void pop(int pos)
{
  int i;
  
  if(pos==-1)
  {
    len--;
    realloc(a,len*sizeof(int));
  }
  else
  {
    
    for(i=pos+1;i<len;i++)
    {
      a[i-1]=a[i];      
    }
    len--;
    realloc(a,len*sizeof(int));
  }
  dis();
}

void ins(int num, int pos)
{
  int i;
  if(pos>=len)
    printf("OUT OF RANGE !");
  else
  {
    len++;
    realloc(a,len*sizeof(int));
    for(i=len;i>pos;i--)
    {
      a[i]=a[i-1];
    }
    a[pos]=num;
  }
  dis();
}

void rem(int num)
{
  int i,pos=-1;
  
  for(i=0;i<len;i++)
  {
    if(a[i]==num)
    {
      pos=i;
      break;
    }
  }
    
  if(pos==-1)
    printf("NOT FOUND !");
  else
  {
    for(i=pos;i<len;i++)
    {
      a[i]=a[i+1];
    }
    len--;
    realloc(a,len*sizeof(int));
  }
  dis();
} 

void rev()
{
  int i,j,tmp;
  
  for(i=0,j=len-1;i<=len/2;i++,j--)
  {
    tmp=a[i];
    a[i]=a[j];
    a[j]=tmp;
  }
  dis();
}
